Ext.namespace("MyPlan");
/*
MyPlan.feeds = {
	marketweekly: null,
	groupweekly: null,
	adminweekly: null,
	busiweekly: null,
	adminmonthly: null
};
MyPlan.initFeed = {
	'1': function(p) {
		if (MyPlan.feeds.marketweekly == null) {
			MyPlan.feeds.marketweekly = new MyPlan.MarketWeeklyFeed(p);
		}
		return MyPlan.feeds.marketweekly;
	},
	'2': function(p) {
		if (MyPlan.feeds.groupweekly == null) {
			MyPlan.feeds.groupweekly = new MyPlan.GroupWeeklyFeed(p);
		}
		return MyPlan.feeds.groupweekly;
	},
	'3': function(p) {
		if (MyPlan.feeds.adminweekly == null) {
			MyPlan.feeds.adminweekly = new MyPlan.AdminWeeklyFeed(p);
		}
		return MyPlan.feeds.adminweekly;
	},
	'4': function(p) {
		if (MyPlan.feeds.busiweekly == null) {
			MyPlan.feeds.busiweekly = new MyPlan.BusiWeeklyFeed(p);
		}
		return MyPlan.feeds.busiweekly;
	},
	'5': function(p) {
		if (MyPlan.feeds.adminmonthly == null) {
			MyPlan.feeds.adminmonthly = new MyPlan.AdminMonthlyFeed(p);
		}
		return MyPlan.feeds.adminmonthly;
	},
};*/
MyPlan.Execute = function() {
	this.winFeedback = null;
	this.winModPlan = null;
	this.winViewPlan = null;
		thiz=this;
	
	this.dtStart = new Ext.form.DateField({
		fieldLabel : '开始时间',
		name : 'startDateFm',
		width : 85,
		format :'Y-m-d'
	});
	this.dtEnd=new Ext.form.DateField({
		fieldLabel:'～',
		name:'startDateTo',
		width: 85,
		format:'Y-m-d'
	});
	this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找计划'},
        handler: this.queryPlan,
        scope: this
    });
    
	this.store = new Ext.data.JsonStore({
	    url: Utils.formatURL('../myplan/executingPlan.form?timesnap=' + Date.parse(new Date())),
	    root: 'result',
	    fields: ['planId', 'planName','createId','createName','auditorId','auditorName','auditNation','planType','startDate','endDate','planState']
	});
	this.store.setDefaultSort('planId', 'desc');
	this.renderType = function(value){
		return OMS.PLANTYPE[value];
	};
	this.renderExport=function(value){
		var url='../auditplan/exportPlan.form?planId='+value+'&timesnap=' + Date.parse(new Date());
		return '<a href="'+url+'" >导出</a>';
	}
	this.column = [
		  {header: '计划编号', sortable: true, align: 'left',hidden:true, dataIndex: 'planId'},		  
		  {header: '计划名称', sortable: true, align: 'left', dataIndex: 'planName'},
	      {header: '计划类型', sortable: true, align: 'center',width: 20, dataIndex: 'planType',renderer:this.renderType},
	      {header: '开始时间', sortable: true, align: 'center', width: 20, dataIndex: 'startDate'},
	      {header: '结束时间', sortable: true, align: 'center', width: 20, dataIndex: 'endDate'},
	      {header: '审核人', sortable: true,width: 40, align: 'left',dataIndex: 'auditorName'},
	      {header: '导出',  align: 'center',width: 40, dataIndex: 'planId',renderer:this.renderExport}
	];	
	this.btnFeedback = new Ext.Button({
		text: '计划反馈',
		iconCls: 'icon-feedback',
		scope: this,
		handler: this.onFeedback
	});
	this.btnModify = new Ext.Button({
		text: '修改计划',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onModify
	});
	MyPlan.Execute.superclass.constructor.call(this, {
		title: '已审核计划',
        region: 'center',
        //tbar: [this.btnFeedback,this.btnModify],
        cm: new Ext.grid.ColumnModel(this.column),
        sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        tbar:['->','计划开始时间:', this.dtStart, '~', this.dtEnd,this.btnSearch],
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true },
        listeners:{
        	rowdblclick : this.onViewPlan,
			//render:this.queryPlan
			activate: this.queryPlan
		}
    });
};
Ext.extend(MyPlan.Execute, Ext.grid.GridPanel, {
	onFeedback: function() {		
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn('请选择一条计划！',"警告");
			return ;
		}
		/*var planType=record.data.planType;
		if (!MyPlan.initFeed[planType]) {
			Utils.info('无效计划类型，请联系管理员!');
			return;
		}		
		this.winFeedback = MyPlan.initFeed[planType](this);*/
		if (this.winFeedback == null) {
			this.winFeedback = new MyPlan.WeeklyFeed(this);
		}
		this.winFeedback.initWin(records[0].data);
		this.winFeedback.show();
	},
	onViewPlan:function(grid, rowIndex,e){		
		if (this.winViewPlan == null) {
			this.winViewPlan = new MyPlan.WeeklyView(this);
		}
		this.winViewPlan.show();
		this.winViewPlan.initWin(grid.store.getAt(rowIndex).data);		
	},
	onModify: function(btn, event) {
		if (this.winModPlan == null) {
			this.winModPlan = new MyPlan.ModifyAudited(this);
		}
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn('请选择一条计划！',"警告");
			return ;
		}else if(records.length>1){
			Utils.warn('只能选择一条计划！',"警告");
			return;
		}
		this.winModPlan.show();
		this.winModPlan.initWin(records[0].data);		
	},
	queryPlan:function(btn,event){
		var param={
			start : 0,
			limit : 15,
			startDateFm:this.dtStart.getValue(),
			startDateTo:this.dtEnd.getValue()
		};
		Utils.formatDate(param,'startDateFm');
		Utils.formatDate(param,'startDateTo');
		this.store.load({
			params:param
		});
	}
});
